import 'dart:async';

import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/api.dart';
import 'package:qgchat/http/models/assessModel.dart';

/// 评价记录bloc
class AssessRecordsBlog implements BlocBase {

  bool isFirstLoading = true; // 是否第一次加载
  int page = 0;
  final int limit = 10;
  List<AssessModel> lists = [];

  final List<AssessModel> defaultData = [];
  final String token;

  AssessRecordsBlog(this.token) {
    getAssessRecordsLists();
  }

  @override
  void dispose() {
    _assessRecords.close();
  }

// Stream ----------------------------------------------------------------------
  /// 评价记录列表
  StreamController<List<AssessModel>> _assessRecords = StreamController.broadcast();
  Sink<List<AssessModel>> get assessRecordsSink => _assessRecords.sink;
  Stream<List<AssessModel>> get assessRecordsStream => _assessRecords.stream;

// method ----------------------------------------------------------------------
  /// 获取评价记录列表
  void getAssessRecordsLists() async {
    page += 1;
    List<AssessModel> result = await UserApis.getAssessRecordsLists(token, page, limit);
    if (result.isNotEmpty) {
      isFirstLoading = false;
      lists.addAll(result);
      assessRecordsSink.add(lists);
    } else {
      if (!isFirstLoading) showToast("已经没有了喔~ww");
      isFirstLoading = false;
      assessRecordsSink.add(lists);
    }
  }

  /// 刷新
  void onRefresh() {
    page = 0;
    lists = [];
    getAssessRecordsLists();
  }

}